﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;

namespace StockStar.GatherData
{
    public static class OperMethod
    {
        /// <summary>
        /// GetPageByUrl
        /// </summary>
        /// <param name="url"></param>
        /// <param name="encoding"></param>
        /// <returns></returns>
        public static string GetPageByUrl(string url, string encoding)
        {
            try
            {
                HttpWebRequest hwr = (HttpWebRequest)WebRequest.Create(url);
                hwr.Method = "GET";
                hwr.KeepAlive = true;
                hwr.Accept = "*/*";
                hwr.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)";
                hwr.Headers.Add("Accept-Language", "zh-cn");
                hwr.Headers.Add("UA-CPU", "x86");
                hwr.Headers.Add("Cookie", "JSESSIONID=1DCDFF4036AD4EFE47A212E22FD8EFC1");
                //hwr.Referer = "http://www.cnstock.com";
                hwr.ServicePoint.Expect100Continue = false;
                HttpWebResponse response = (HttpWebResponse)hwr.GetResponse();
                Stream myResponseStreame = response.GetResponseStream();
                StreamReader sdr = new StreamReader(myResponseStreame, Encoding.GetEncoding(encoding));
                return sdr.ReadToEnd().ToString();
            }
            catch
            {
                return "";
            }
        }

        /// <summary>
        /// StrReplace
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string StrReplace(string str)
        {
            return str.Replace("\r", "").Replace("\n", "").Replace("\t", "").Trim();
        }

        /// <summary>
        /// StrReplaceHtml
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string StrReplaceHtml(string str)
        {
            str = Regex.Replace(str, @"<[^>]+>", "");
            return str.Replace("\r", "").Replace("\n", "").Replace("\t", "").Trim();
        }

        public static string StrReplaceNum(string str)
        {
            MatchCollection mcn = Regex.Matches(str, "-{0,1}\\d+(.\\d+){0,}");
            if (mcn.Count > 0)
                return mcn[0].Value;
            else
                return "0";
        }
        /// <summary>
        /// CreatePath
        /// </summary>
        /// <param name="strPath"></param>
        /// <returns></returns>
        public static bool CreatePath(string strPath)
        {
            //D:\Work\采集\QuoteData\TSymbolist.xml
            try
            {
                if (Directory.Exists(strPath))
                    return true;
                else
                {
                    Directory.CreateDirectory(strPath);
                }
            }
            catch
            {
                LogFile.WriteLog("目录(" + strPath + ")创建失败!");
                return false;
            }
            return true;
        }


        public static bool DelFile(string strPath)
        {
            //D:\Work\采集\QuoteData\TSymbolist.xml
            File.Delete(strPath);
            return true;
        }

        /// <summary>
        /// PathExit
        /// </summary>
        /// <returns></returns>
        public static bool PathExit()
        {
            string _xml_Hx_FullPath = System.Configuration.ConfigurationSettings.AppSettings["xml_HX_Path"].ToString();
            string _xml_Hx_Path = _xml_Hx_FullPath.Substring(0, _xml_Hx_FullPath.LastIndexOf("\\")) + "\\";
            if (CreatePath(_xml_Hx_Path) == false)
                return false;

            string _xml_Qa_FullPath = System.Configuration.ConfigurationSettings.AppSettings["xml_QA_Path"].ToString();
            string _xml_Qa_Path = _xml_Qa_FullPath.Substring(0, _xml_Qa_FullPath.LastIndexOf("\\")) + "\\";
            if (CreatePath(_xml_Qa_Path) == false)
                return false;

            string _xml_Log_Path = System.Configuration.ConfigurationSettings.AppSettings["LogPath"].ToString();
            if (CreatePath(_xml_Log_Path) == false)
                return false;

            return true;
        }

        public static string FormatHtml(string _content)
        {
            _content = Regex.Replace(_content, "<script[^>]{0,}>[\\S\\s]*?</script>", "", RegexOptions.IgnoreCase);
            _content = Regex.Replace(_content, "<frameset[^>]{0,}>[\\S\\s]*?</frameset>", "", RegexOptions.IgnoreCase);
            _content = Regex.Replace(_content, "<iframe[^>]{0,}>[\\S\\s]*?</iframe>", "", RegexOptions.IgnoreCase);
            _content = Regex.Replace(_content, "<style[^>]{0,}>[\\S\\s]*?</style>", "", RegexOptions.IgnoreCase);
            _content = Regex.Replace(_content, "<!--[\\S\\s]*?-->", "", RegexOptions.IgnoreCase);
            _content = _content.Replace("'", "''");
            return _content;
        }

        public static Decimal ToDeceimal(string obj)
        {
            decimal rel = 0;
            if (!string.IsNullOrEmpty(obj))
            {
                try
                {
                    obj = obj.Replace(",", "").Trim();
                    Regex regNum = new Regex("\\d+(.\\d+){0,}");
                    obj = regNum.Match(obj).Value;
                    rel = decimal.Parse(obj);
                }
                catch
                {
                }
            }
            return rel;
        }

        public static DateTime ToDate(string obj, DateTime _dt)
        {
            DateTime rel = _dt;
            if (!string.IsNullOrEmpty(obj))
            {
                try
                {
                    rel = DateTime.Parse(obj);
                }
                catch
                {
                }
            }
            return rel;
        }

        public static int ToInt(string obj)
        {
            int rel = 0;
            if (!string.IsNullOrEmpty(obj))
            {
                try
                {
                    obj = obj.Replace(",", "").Trim();
                    Regex regNum = new Regex("\\d+(.\\d+){0,}");
                    obj = regNum.Match(obj).Value;
                    rel = int.Parse(obj);
                }
                catch
                {
                }
            }
            return rel;
        }

        public static long ToLong(string obj) 
        {
            long rel = 0;
            if (!string.IsNullOrEmpty(obj))
            {
                try
                {
                    obj = obj.Replace(",", "").Trim();
                    Regex regNum = new Regex("\\d+(.\\d+){0,}");
                    obj = regNum.Match(obj).Value;
                    rel = long.Parse(obj);
                }
                catch
                {
                }
            }
            return rel;
        }




    }
}
